import createDomElement from './createDomElement';
import mountElement from './mountElement';
import unmountNode from './unmountNode';

export default function mountNativeElement(vDom, container, oldDom, insert = false) {
  let newElement = createDomElement(vDom);
  if (oldDom) {
    container.insertBefore(newElement, oldDom);
  } else {
    container.appendChild(newElement);
  }

  // 判断旧的dom是否存在 如果存在 删除
  if (oldDom && !insert) unmountNode(oldDom);

  let component = vDom.component;
  if (component) {
    // 把真实节点绑定到虚拟Dom 的 _Dom 属性上
    component.setDom(newElement);
  }
}
